package com.gry.stusys;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class App {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作1登录,2注册,3忘记密码,0退出");
            String choose = sc.next();
            switch (choose) {
                case "1":
                    login(list);
                    break;
                case "2":
                    register(list);
                    break;
                case "3":
                    forgetPassword(list);
                    break;
                case "0":
                    System.out.println("退出");
                    System.exit(0);
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }
    }

    private static void login(ArrayList<User> list) {
        System.out.println("登录");
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("输入用户名");
            String username = sc.next();

        }
    }

    private static void forgetPassword(ArrayList<User> list) {
        System.out.println("忘记密码");
    }

    private static void register(ArrayList<User> list) {
        System.out.println("注册");
        Scanner sc = new Scanner(System.in);
        //用户名:
        String username;
        String password;
        String personID;
        String phoneNum;
        while (true) {
            System.out.println("请输入新建的用户名");
            username = sc.next();
            boolean flag = checkUsername(username);
            if (!flag) {
                System.out.println("输入有误,请重新输入");
                continue;
            }
            boolean flag2 = contains(list, username);
            if (flag2) {
                //代表用户已存在;
                System.out.println("用户已存在!");
            } else {
                System.out.println(username + "可使用");
                break;
            }
        }
        //密码:
        while (true) {
            System.out.println("请输入新建的密码");
            password = sc.next();
            System.out.println("确认密码");
            String second = sc.next();
            if (password.equals(second)) {
                System.out.println("可使用");
                break;
            } else {
                System.out.println("密码错误");
            }
        }
        //身份证:
        while (true) {
            System.out.println("输入身份证号");
            personID = sc.next();
            boolean flag = checkPersonID(personID);
            if (flag) {
                System.out.println("成功");
                break;
            } else {
                System.out.println("身份证号码错误");
            }
        }
        //手机号:
        while (true) {
            System.out.println("输入手机号");
            phoneNum = sc.next();
            boolean flag = checkPhoneNum(phoneNum);
            if (flag) {
                System.out.println("正确");
                break;
            } else {
                System.out.println("输入错误,请重新输入");
            }
        }
        User user = new User(username, password, personID, phoneNum);
        list.add(user);
        printList(list);
    }

    //生成验证码:
    private static String verificationCode() {
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }
        Random r = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(list.size());
            char c = list.get(index);
            sb.append(c);
        }
        sb.append(r.nextInt(10));
        char[] charArray = sb.toString().toCharArray();
        int index = r.nextInt(charArray.length);
        char temp = charArray[index];
        charArray[index] = charArray[charArray.length - 1];
        charArray[charArray.length - 1] = temp;
        return new String(charArray);

    }

    private static void printList(ArrayList<User> list) {
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getUsername() + "," + list.get(i).getPassword() +
                    "," + list.get(i).getIdcard() + "," + list.get(i).getPhonenumber());
        }
    }


    private static boolean checkPhoneNum(String phoneNum) {
        if (phoneNum.length() != 11) {
            return false;
        }
        if (phoneNum.charAt(0) == 0) {
            return false;
        }
        for (int i = 0; i < phoneNum.length(); i++) {
            char c = phoneNum.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }
        return true;
    }

    private static boolean checkPersonID(String personID) {
        if (!(personID.length() == 18)) {
            return false;
        }
        if (personID.charAt(0) == '0') {
            return false;
        }
        for (int i = 0; i < personID.length() - 1; i++) {
            if (!(personID.charAt(i) >= '0' && personID.charAt(i) <= '9')) {
                return false;
            }
        }
        char endChar = personID.charAt(personID.length() - 1);
        if ((endChar >= '0' && endChar <= '9') || endChar == 'X' || endChar == 'x') {
            return true;
        } else {
            return false;
        }
    }

    private static boolean contains(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUsername().equals(username)) {
                return true;
            }
        }
        return false;
    }

    private static boolean checkUsername(String username) {
        int length = username.length();
        //判断用户名长度必须在3~15为之间;
        if (!(length > 3 && length < 15)) {
            return false;
        }
        //判断用户名中只能是字母加数字组合.
        for (int i = 0; i < length; i++) {
            char c = username.charAt(i);
            if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9')) {
                return false;
            }
        }
        int count = 0;
        for (int i = 0; i < length; i++) {
            char c = username.charAt(i);
            if (c >= 'a' && c < 'z' || c >= 'A' && c <= 'Z') {
                count++;
                break;
            }
        }
        return count > 0;
    }

}
